#!/usr/bin/env python3
# -*- coding:utf-8 -*-
# @Time: 2019/5/26  19:58
# @Author: 余浪人
# @email: yulangren520@gmail.com
import re

from apps.api import api_bp
from flask import jsonify, request
from apps.lib.token import verify_token
from apps.lib.public import api_check_obj
from apps.models.column_model import Column_model
from apps.models.document_model import Document_Model
from apps.models.member_model import Member, db


# 会员添加/修改
@api_bp.route('/member', methods=['PUT', 'POST'])
@verify_token
def member():
    data = request.json.get('data')
    if request.method == 'POST' and request.is_json and data:
        if Member.query.filter(Member.email == data.get('email')).count(): return jsonify(
            {"status": 201, "message": "用户已存在!(邮箱)"})
        try:
            obj = Member()
            obj.set_attrs(data)
            db.session.add(obj)
            db.session.commit()
        except:
            db.session.rollback()
            return jsonify({"status": 401, "message": "添加异常!"})
        return jsonify({"status": 0, "message": "添加成功!"})
    elif request.method == 'PUT' and request.is_json:
        uuid = data.get('user_key')
        obj = Member.query.filter_by(uuid=uuid).first()
        if api_check_obj(obj): return api_check_obj(obj)
        try:
            obj.set_attrs(data)
            db.session.commit()
        except:
            db.session.rollback()
            return jsonify({"status": 401, "message": "更新数据异常!"})
        return jsonify({"status": 0, "message": "更新数据成功!"})
    return jsonify({"status": 400, "message": "请求异常!"})


# 获取分类
@api_bp.route('/column', methods=['POST'])
@verify_token
def column():
    ColumnList = Column_model.query.all()
    data = {
        "status": 0,
        "message": "获取分类成功",
        "columnList": [
            {
                "column_name": col.column_name,
                "column_id": col.id,
                "column_uuid": col.uuid,
                "column_order": col.column_order,
            }
            for col in ColumnList
        ]
    }
    return jsonify(data)


# 文档发布
@api_bp.route('/document', methods=['POST'])
@verify_token
def document():
    data = request.json.get('data')
    if data and request.is_json:
        if Document_Model.query.filter(Document_Model.title == data.get('title')).count():
            return jsonify({"status": 400, "message": "文档标题已存在!"})
        if api_check_obj(Column_model.query.filter_by(id=data.get('superior')).first()):
            return jsonify({"status": 404, "message": "分类栏目不存在!"})
        try:
            document = Document_Model()
            document.set_attrs(data)
            db.session.add(document)
            db.session.commit()
        except:
            db.session.rollback()
            return jsonify({"status": 401, "message": "发布异常!"})
        return jsonify({"status": 0, "message": "发布成功!"})
    return jsonify({"status": 403, "message": "请求异常!"})


# 删除分类
@api_bp.route('/column', methods=['DELETE'])
@verify_token
def delete_column():
    data = request.json.get('data')
    column_id = data.get('column_id')
    if request.is_json and data and isinstance(column_id, int):
        col_obj = Column_model.query.filter_by(id=column_id).first()
        if api_check_obj(col_obj): return api_check_obj(col_obj)
        try:
            db.session.delete(col_obj)
            db.session.commit()
        except:
            db.session.rollback()
            return jsonify({"status": 400, "message": "删除异常"})
        return jsonify({"status": 0, "message": "删除成功"})
    return jsonify({"status": 401, "message": "请求异常"})
